<?php

namespace App\Http\Controllers\Admin;

use App\Model\Config;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class ConfigController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $conf = Config::get();

        foreach ($conf as $v){
            switch ($v->field_type){
                //1. input
                case 'input' :
                    $v->conf_contents = '<input type="text" value="'.$v->conf_content.'" name="conf_content[]"   class="layui-input">';

                    break;
                //2. textarea
                case 'textarea' :
                    $v->conf_contents = '<textarea  class="layui-textarea" name="conf_content[]">'.$v->conf_content.'</textarea>';

                    break;
                //3.rodio
                case 'radio' :
                    $str = '';

                    $arr= explode(',',$v->field_value);

                    foreach ($arr as $n){
                        $a = explode('|',$n);
                        if ($a[0] == $v->conf_content){

                            $str .= '<input type="radio" name="conf_content[]" value="'.$a[0].'" title="'.$a[1].'" checked>'.$a[1].'&nbsp;&nbsp;&nbsp;' ;
                        }else{
                            $str .= '<input type="radio" name="conf_content[]" value="'.$a[0].'" title="'.$a[1].'" >'.$a[1].'&nbsp;&nbsp;&nbsp;';

                        }
                    }
                    $v->conf_contents = $str;

                    break;
            }
        }
        return view('admin.config.list',compact('conf'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.config.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
       $input = $request->except('_token');
       $res = Config::create($input);

        if ($res){
            $this->putConfigs();
            $data = [
                'status'=>0,
                'message'=>'成功'
            ];
        }else{
            $data = [
                'status'=>1,
                'message'=>'失败'
            ];
        }
        return $data;
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $res = Config::where('conf_id',$id)->delete();
        if ($res){

            $this->putConfigs();
            $data = [
                'status'=>0,
                'message'=>'成功'
            ];
        }else{
            $data = [
                'status'=>1,
                'message'=>'失败'
            ];
        }
        return $data;
    }

    public function changeContent(Request $request)
    {
        $input = $request->except('_token');
        //开启事务
        DB::beginTransaction();

        try{
            foreach($input['conf_id'] as $k => $v){
                DB::table('config')->where('conf_id',$v)->update(['conf_content'=>$input['conf_content'][$k]]);
            }
            DB::commit();
            $this->putConfigs();
            return redirect('/admin/config');

        }catch(\Exception $e){
            DB::rollBack();
            return redirect()->back()->withErrors(['error'=>$e->getMessage()]);
        }
    }
    //将网站数据写入config/webconfig中
    public function putConfigs()
    {
         //1.从数据库中获取网站配置
        $config = Config::pluck('conf_content','conf_name')->all();
        //准备写入的内容
        $str = '<?php return '.var_export($config,true).';';

        //将内容写入文件

        file_put_contents(config_path().'/webconfigs.php',$str);
    }
}
